// pages/index/indexDetail.js
var WxParse = require('../../../utils/wxParse/wxParse.js');
var {emoji} = require('../../common/emoji/emoji-mine.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailId: 'id',
    detailData: null,
    article: '',
    commentList: null,
    commentPage: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      detailId: options.id
    }) 
    this.getDetailData()
    this.getCommentData()
  },

  getDetailData: function() {
    var that = this
    app.fetchPostApi('news/getAppNewsDetails', { "newsId": this.data.detailId }).then(data => {
      data.createTime = app.formatTime(data.createTime, 'Y.M.D h:m')
      for (var i = 0; i < data.recommends.length; i++) {
        data.recommends[i].logo[0] = app.globalData.base_image_url + data.recommends[i].logo[0]
        data.recommends[i].createTime = app.changeTime(data.recommends[i].createTime)
      }
      that.setData({
        detailData: data
      })
      WxParse.wxParse('article', 'html', data.content, that, 5)

      console.log(data)
    })
  },

  goHomeDetail: function (e) {
    console.log(e.currentTarget.dataset.id)
    var nextUrl = './indexDetail?id=' + e.currentTarget.dataset.id
    wx.navigateTo({
      url: nextUrl,
    })
  },
  getCommentData: function() {
    var that = this
    app.fetchPostApi('news/reply', {
      "id": that.data.detailId,
      "type": 1,
      "page": that.data.commentPage,
      "userSize": 5,
      "size": 3,
    }).then(data => {

      for (var i=0; i<data.length; i++) {
        data[i].createTime = app.changeTime(data[i].createTime)
        if (data[i].user.headerImg.indexOf("http") == 0) {
        } else {
          data[i].user.headerImg = app.globalData.base_image_url + data[i].user.headerImg
        }
        data[i].content = emoji(data[i].content)
        WxParse.wxParse('comment' + i, 'html', data[i].content, that, 5)
        var replys = data[i].replyIds
        if (replys) {
          for (var j = 0; j < replys.length; j++) {
            replys[j].createTime = app.changeTime(replys[j].createTime)
            // replys[i].content = emoji(replys[i].content)
            // WxParse.wxParse('reply' + i, 'html', replys[i].content, that, 5)
          }
        }
      }
      that.setData({
        commentList: data
      })
    })
    
  },













  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})